import * as constants from "./constants";
import axios from "axios";
import { fromJS } from "immutable";

// 默认 payload 是普通数组，但是在 reducer.js 中 set('list', payload) 的 list 要是 immutable 对象
// 所以在传递过去之前先把它设置成 immutable 对象
const changeList = (list) => ({
  type: constants.CHANGE_SEARCH_LIST,
  payload: fromJS(list)
})

export const searchFocus = () => ({
  type: constants.SEARCH_FOCUS
});

export const searchBlur = () => ({
  type: constants.SEARCH_BLUR
});

// 可以返回函数，接收dispatch
export const getSearchList = () => {
  return (dispatch) => {
    axios.get('/api/headerList.json').then(response => {
      const { data: { data } } = response
      dispatch(changeList(data.list));
    }).catch((error)=> {
      console.log('error', error);
    })
  }
}

export const listMouseEnter = () => ({
  type: constants.CHANGE_LIST_MOUSEIN
})

export const listMouseLeave = () => ({
  type: constants.CHANGE_LIST_MOUSEOUT
})

export const changePage = (page) => ({
  type: constants.CHANGE_LIST_PAGE,
  payload: page
})